from django.db import models
from users.models import UserModel, DepartmentModel
import datetime


# 审批层次
class LevelModel(models.Model):
    name = models.CharField(verbose_name="审批称此名称", max_length=50)
    level = models.CharField(verbose_name="审批层次", max_length=50)
    currentvetting = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name="审批人")
    is_delete = models.BooleanField(verbose_name="是否删除",default=False)
    level_self = models.ForeignKey("self", on_delete=models.CASCADE, verbose_name="审批子自联", null=True, blank=True)

    class Meta:
        db_table = "level_tb"
        verbose_name = "审批表"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


# 类别
class TypeModel(models.Model):
    name = models.CharField(verbose_name="类型名称", max_length=35)
    template_id = models.IntegerField(null=True, verbose_name="模板名称")
    lastervisename = models.ForeignKey(UserModel, on_delete=models.CASCADE, null=True, blank=True, verbose_name="最后的修改人")
    upd_time = models.DateTimeField(verbose_name="审批时间", auto_now_add=True, null=True, blank=True)
    is_delete = models.BooleanField(verbose_name="是否删除",default=False)

    class Meta:
        db_table = "type_tb"
        verbose_name = "类别表"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


# 流程表
class FlowModel(models.Model):
    flow_name = models.CharField(verbose_name="流程名称", max_length=25)
    peocess = models.CharField(verbose_name="流程说明", max_length=100,null=True)
    currentvetting = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name="审批人", null=True)
    department = models.ForeignKey(DepartmentModel, on_delete=models.CASCADE, verbose_name="部门id", null=True, blank=True)
    is_enable = models.BooleanField(verbose_name="是否启用", default=True)
    level = models.ForeignKey(LevelModel,verbose_name="审批层次", on_delete=models.CASCADE)
    start_time = models.DateTimeField(verbose_name="创建时间", auto_now_add=True)
    is_delete = models.BooleanField(verbose_name="是否删除",default=False)

    class Meta:
        db_table = "flow_tb"
        verbose_name = "流程表"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.flow_name


# 流程节点
class FlowNodeModel(models.Model):
    # 审批类型 vettype 所属部门   true   流程flow
    STATE_OPTIONS = (
        (0, "未审批"), (1, "同意"), (2, "驳回")
    )
    vettypes = models.ForeignKey(TypeModel, on_delete=models.CASCADE, verbose_name="所属类别")
    flow = models.ForeignKey(FlowModel, on_delete=models.CASCADE, verbose_name="流程")
    user_name = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name="发起人")
    # node_name = models.CharField(verbose_name="流程节点名称", max_length=25)
    vettsatus = models.CharField(max_length=10, choices=STATE_OPTIONS, verbose_name="审批状态", default=0)
    examination_why = models.CharField(verbose_name="审批原因", max_length=1000, null=True, blank=True)
    level = models.ForeignKey(LevelModel,verbose_name="当前节点(审批层次)", on_delete=models.CASCADE)
    why = models.CharField(verbose_name="请求", max_length=100, null=True, blank=True)
    start_time = models.DateTimeField(verbose_name="创建时间", auto_now_add=True)
    vettime = models.DateTimeField(verbose_name="审批时间", auto_now=True, null=True, blank=True)
    is_delete = models.BooleanField(verbose_name="是否删除",default=False)

    class Meta:
        db_table = "flow_node_model"
        verbose_name = "流程节点表"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.flow.flow_name


# 用户流程节点表
class UserFlowNodeModel(models.Model):
    # user_node_name = models.CharField(verbose_name="用户节点的名称", max_length=25)
    name = models.ForeignKey(UserModel, verbose_name="用户名", on_delete=models.CASCADE)
    flow = models.ForeignKey(FlowNodeModel, on_delete=models.CASCADE, verbose_name="流程节点")
    is_delete = models.BooleanField(verbose_name="是否删除",default=False)

    class Meta:
        db_table = "user_flow_node_tb"
        verbose_name = "用户流程节点表"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name.name


# 节点信息表
class NodeInformationModel(models.Model):
    describe = models.CharField(verbose_name="描述", max_length=99)
    # flow = models.ForeignKey(FlowModel, on_delete=models.CASCADE, verbose_name="流程")
    flow_node = models.ForeignKey(FlowNodeModel, on_delete=models.CASCADE, verbose_name="流程节点")
    is_through = models.BooleanField(verbose_name="是否通过", default=False)
    is_delete = models.BooleanField(verbose_name="是否删除",default=False)

    class Meta:
        db_table = "node_information_tb"
        verbose_name = "节点信息表"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.describe


# 历史审批
class HistoryExaminationModel(models.Model):
    # flow = models.ForeignKey(FlowModel, on_delete=models.CASCADE, verbose_name="发起流程")
    flow_node = models.ForeignKey(FlowNodeModel, on_delete=models.CASCADE, verbose_name="当前节点")
    # end_data = models.DateTimeField(verbose_name="时间", default=datetime.datetime.now())
    is_delete = models.BooleanField(verbose_name="是否删除",default=False)

    class Meta:
        db_table = "history_examination_tb"
        verbose_name = "历史表"
        verbose_name_plural = verbose_name

    def __str__(self):
        return "发起人：%s, 发起流程：%s" % (self.flow_node.user_name, self.flow_node.user_name)


# # 类型
# class ClassTypeModel(models.Model):
#     name = models.CharField(verbose_name="类型名称", max_length=20)
#
#     class Meta:
#         db_table = "class_type_tb"
#         verbose_name = "类型"
#         verbose_name_plural = verbose_name
#
#     def __str__(self):
#         return self.name




